/*
 * PingerTask.java
 *
 * Created on 16 December 2006, 09:59
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hubsniffer.sghdc.tasks;

import hubsniffer.ConnectHubThread;
import hubsniffer.DCHub;
import libjdc.dc.client.User;
import hubsniffer.conn.HubInfoConnection;
import hubsniffer.db.DataBaseManager;
import hubsniffer.db.HubINFOWriter;
import hubsniffer.util.Host;
import libjdc.dc.log.Log;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Stack;

/**
 *
 * @author root
 */
public class PingerTask implements Runnable{
    
    private long delay;
    private DataBaseManager db;
    private Date start;
    private LinkedList<DCHub> hublist;
    
    public PingerTask(long delay,Date start,DataBaseManager db,LinkedList<DCHub> hublist){
        this.start = start;
        this.db = db;
        this.hublist = hublist;
        this.delay = delay;
    }
    
        /* (non-Javadoc)
         * @see java.util.TimerTask#run()
         */
    public void run() {
        Iterator<DCHub> hubIt = hublist.iterator();
        start = new Date();
        
        Stack<ConnectHubThread> hubThreads = new Stack<ConnectHubThread>();
        
        while (hubIt.hasNext()) {
            DCHub hub = hubIt.next();
            User self= new User("[PT]sniffer","{HubSniffer Project: http://sghdc.no-ip.org}", "DSL","admin.sghdc@gmail.com", 104219547999L);                self.setHost(Host.getHostIP());
            
            /*try {
                self = db.getSnifferUserFromHub(hub.getHub_id());
            } catch (SQLException ex) {
                ex.printStackTrace(Sniffer.err);
                self = new User("[PT]sniffer","{HubSniffer Project: http://sghdc.no-ip.org/listhubs.php} <++ V:0.674,M:A,H:1/0/0,S:3>", "DSL","admin.sghdc@gmail.com", 104219547999L);                self.setHost(Host.getHostIP());
            }*/
            self.setHost(Host.getHostIP());
            
                hubThreads.push(new ConnectHubThread(new HubInfoConnection(hub,self),5000));
                hubThreads.peek().start();
            
        }
        
        while(!hubThreads.empty()){
            try {
                ConnectHubThread cht = hubThreads.pop();
                Log.out.println(hubThreads.size()+":"+cht.getHubConnection().getHub()+" Waiting..");
                cht.join();
                Log.out.println(hubThreads.size()+":"+cht.getHubConnection().getHub()+"Closed!");
                try {
                    new HubINFOWriter(db).updateHubInfo(cht.getHubConnection().getHub());
                } catch (UnsupportedEncodingException ex) {
                    ex.printStackTrace(Log.err);
                } catch (SQLException ex) {
                    ex.printStackTrace(Log.err);
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace(Log.err);
            }
        }
    }
    
}
